package com.gitee.wsl.unit.angle.ext

import com.gitee.wsl.unit.angle.AngleUnit
import com.gitee.wsl.unit.angle.deg
import com.gitee.wsl.unit.angle.toDegrees
import kotlin.math.abs

fun AngleUnit.shrinkAngle(): AngleUnit {
    val deg = degrees
    return (if (deg > 180.0) {
        (deg % 180.0) - 180.0
    } else if (deg < -180.0) {
        (deg % 180.0) + 180.0
    } else if (abs(deg) == 180.0) {
        0.0
    } else {
        deg
    }).deg
}

/**
 * Sanitizes a degree measure as a floating-point number.
 *
 * @return a degree measure between 0.0 (inclusive) and 360.0 (exclusive).
 */
fun AngleUnit.sanitize360(): AngleUnit {
    var degrees = degrees % 360
    if (degrees < 0) {
        degrees += 360.0
    }
    return degrees.toDegrees()
}
